package com.knight.mall.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.knight.mall.common.pageUtils.PageResult;
import com.knight.mall.dto.CollectTimeDto;
import com.knight.mall.mappers.CollectPlaceMapper;
import com.knight.mall.mappers.EnterpriseMapper;
import com.knight.mall.mappers.TradeMapper;
import com.knight.mall.model.CollectPlace;
import com.knight.mall.model.Enterprise;
import com.knight.mall.model.Trade;
import com.knight.mall.query.CollectPlaceQuery;
import com.knight.mall.service.CollectPlaceService;
import com.knight.mall.utils.GenerateUtil;


@Service(value = "collectPlaceService")
public class CollectPlaceServiceImpl implements CollectPlaceService {

	private static final String FORMAT = "yyyyMMddHHmmss";
	
	@Autowired
	private CollectPlaceMapper collectPlaceMapper;
	@Autowired
	private TradeMapper tradeMapper;
	@Autowired
	private EnterpriseMapper enterpriseMapper;
	
	@Override
	public CollectPlace load(Long id) {
		return collectPlaceMapper.load(id);
	}

	@Override
	public Long add(CollectPlace collectPlace) {
		collectPlaceMapper.insert(collectPlace);
		return collectPlace.getId();
	}

	@Override
	public int update(CollectPlace collectPlace) {
		return collectPlaceMapper.update(collectPlace);
	}

	@Override
	public int delete(Long id) {
		return collectPlaceMapper.delete(id);
	}

	@Override
	public List<CollectPlace> queryList(CollectPlaceQuery collectPlaceQuery) {
		return collectPlaceMapper.queryList(collectPlaceQuery);
	}

	@Override
	public int queryCount(CollectPlaceQuery collectPlaceQuery) {
		return collectPlaceMapper.queryCount(collectPlaceQuery);
	}

	@Override
	public int deleteByIdAndEnterpriseId(CollectPlace collectPlace) {
		return collectPlaceMapper.deleteByIdAndEnterpriseId(collectPlace);
	}

	@Override
	public CollectTimeDto getCollectTime(Long collectPlaceId) {
		CollectTimeDto collectTimeDto = null;
		CollectPlace collectPlace = collectPlaceMapper.load(collectPlaceId);
		if (collectPlace != null) {
			Enterprise enterprise = enterpriseMapper.load(collectPlace.getEnterpriseId());
			if (enterprise != null) {
				collectTimeDto = new CollectTimeDto();
				boolean b = true;
				int d = collectPlace.getCollectTime()/24;
				Date date = new Date();
				if (b) {
					d = d + 1;
				}
				collectTimeDto.setStartTime(new Date());
				collectTimeDto.setEndTime(new Date());
			}
		}
		return collectTimeDto;
	}
	@Override
	public List<CollectTimeDto> getCollectTimeList(List<CollectTimeDto> list) {
		// TODO Auto-generated method stub
		if (CollectionUtils.isNotEmpty(list)) {
			CollectPlace collectPlace = null;
			Trade trade = null;
			Enterprise enterprise = null;
			for(CollectTimeDto c: list) {
				trade = tradeMapper.load(c.getTradeId());
				collectPlace = collectPlaceMapper.load(trade.getBuyerAddressId());
				enterprise = enterpriseMapper.load(trade.getEnterpriseId());
				boolean b = true;
				int d = collectPlace.getCollectTime()/24;
				Date date = new Date();
				if (b) {
					d = d + 1;
				}
				c.setStartTime(new Date());
				c.setEndTime(new Date());
			}
		}
		return list;
	}


	@Override
	public PageResult<CollectPlace> queryListPage(CollectPlaceQuery query) {
		List<CollectPlace> list = this.queryList(query);
		int count = this.queryCount(query);
		return PageResult.create(query, list, count);
	}
}
